<?php
function getSideBar($elements)
{
  $sidebar = '
<div id="sidebar">
  <div class="list-group">
  ';
  
  foreach ($elements as $element)
  {
    $sidebar .= getSideBarElement($element);
  }
  
//     <a class="list-group-item" href="#libraries">
//       Libraries
//     </a>
//     <a class="list-group-item" data-toggle="collapse"
//         data-target="#sidebar-glfw-error-callback" href="#code">
//       Code <b class="caret"></b>
//     </a>
//     <div id="sidebar-glfw-error-callback" class="collapse list-group-second-level" >
//       <a class="list-group-item"
//           href="#glfw-error-callback">
//         GLFW Error Callback
//       </a>
//     </div>

  $sidebar = $sidebar . '
  </div>
</div>
  ';
  return $sidebar;
}

function getSideBarElement($element)
{
  $has_child = array_key_exists('data-target', $element);
  $child_toggle = '';
  if ($has_child)
  {
    $child_toggle = ' data-toggle="collapse" 
      data-target="#' . $element['data-target'] . '"';
  }
  
  $elem_html .= '
      <a class="list-group-item" href="' . $element["anchor"] . '"' . $child_toggle . '>
        ' . $element["text"] . ($has_child ? ' <b class="caret"></b>' : '') . '
      </a>
    ';
  
  if ($has_child)
  {
    $elem_html .= '
        <div id="' . $element['data-target'] . '" 
          class="collapse list-group-second-level" >
      
      ';
    
    foreach ($element['data-target-elems'] as $child)
      $elem_html .= getSideBarElement($child);
    
    $elem_html .= '
        </div>
      ';
  }
  
  return $elem_html;
}
?>